/*:
 * @plugindesc (v1.01)法宝升级提示
 * @author 小c
 * @version 1.0.1
 * @date 1/18/2025
 *
 * @param Window X
 * @text 窗口X
 * @type note
 * @default "return Graphics.boxWidth / 4;"
 *
 * @param Window Y
 * @text 窗口Y
 * @type note
 * @default "return Graphics.boxHeight / 8;"
 *
 * @param Window Width
 * @text 窗口宽度
 * @type note
 * @default "return Graphics.boxWidth / 2;"
 *
 * @param Window Lines
 * @text 窗口最大行数
 * @type number
 * @min 1
 * @default 5
 *
 * @param Info Time
 * @text 信息维持时间
 * @type number
 * @min 1
 * @default 120
 * @desc 单位：帧
 *
 * @param Background Color1
 * @text 背景颜色1
 * @type number
 * @min -1
 * @default 7
 * @desc 背景颜色有一个为-1，则没有背景
 *
 * @param Background Color2
 * @text 背景颜色2
 * @type number
 * @min -1
 * @default 8
 * @desc 背景颜色有一个为-1，则没有背景
 *
 * @param Text Format
 * @text 提示格式
 * @default %1 提升至 %2 级！
 *
 * @param Text Color
 * @text 提示文字颜色
 * @type number
 * @default 0
 *
 * @help
 * 显示法宝升级提示
 * 窗口区域 颜色 显示文字内容 请配置插件参数
 *
 * 注意：背景颜色是由颜色2-颜色1-颜色2的渐变
 * 
 * 更新日志
 * v1.00
 * 老饕：修改UI效果
 * v1.00
 * 创建完成
 * 
 */

var Imported = Imported || {};
Imported.CP_X_MagicWeaponLevelUpInfo = true;

var CP = CP || {};
CP.MagicWeaponLevelUpInfo = CP.MagicWeaponLevelUpInfo || {};
CP.MagicWeaponLevelUpInfo.Params = PluginManager.parameters("CP_X_MagicWeaponLevelUpInfo");
CP.MagicWeaponLevelUpInfo.WINDOW_RECT = {
	x: new Function(JSON.parse(CP.MagicWeaponLevelUpInfo.Params["Window X"])),
	y: new Function(JSON.parse(CP.MagicWeaponLevelUpInfo.Params["Window Y"])),
	width: new Function(JSON.parse(CP.MagicWeaponLevelUpInfo.Params["Window Width"])),
	maxLines: Number(CP.MagicWeaponLevelUpInfo.Params["Window Lines"]) || 1
};
CP.MagicWeaponLevelUpInfo.INFO_TIME = Number(CP.MagicWeaponLevelUpInfo.Params["Info Time"]) || 1;
CP.MagicWeaponLevelUpInfo.BACKGROUND_COLOR1 = Number(CP.MagicWeaponLevelUpInfo.Params["Background Color1"]) || 0;
CP.MagicWeaponLevelUpInfo.BACKGROUND_COLOR2 = Number(CP.MagicWeaponLevelUpInfo.Params["Background Color2"]) || 0;
CP.MagicWeaponLevelUpInfo.TEXT_FORMAT = CP.MagicWeaponLevelUpInfo.Params["Text Format"].trim();
CP.MagicWeaponLevelUpInfo.TEXT_COLOR = Number(CP.MagicWeaponLevelUpInfo.Params["Text Color"]) || 0;

CP.MagicWeaponLevelUpInfo._loaded = false;
CP.MagicWeaponLevelUpInfo.DATABASE_LOADED = DataManager.isDatabaseLoaded;
DataManager.isDatabaseLoaded = function(){
	if(!CP.MagicWeaponLevelUpInfo.DATABASE_LOADED.call(this))
		return false;
	if(!CP.MagicWeaponLevelUpInfo._loaded){
		CP.MagicWeaponLevelUpInfo.loadCustomLevelUpInfo($dataWeapons);
		CP.MagicWeaponLevelUpInfo.loadCustomLevelUpInfo($dataArmors);
		CP.MagicWeaponLevelUpInfo._loaded = true;
	}

	return true;
};

CP.MagicWeaponLevelUpInfo.loadCustomLevelUpInfo = function(group){
	for(var i = 1; i < group.length; i++){
		var equip = group[i];
		equip.magicWeaponLevelUpInfo = this.TEXT_FORMAT;
		if(equip.meta["Magic Weapon LevelUp Info"])
			equip.magicWeaponLevelUpInfo = equip.meta["Magic Weapon LevelUp Info"].trim();
	}
};

//------------------------------
// Game_Temp
//------------------------------
CP.MagicWeaponLevelUpInfo.TEMP_INIT = Game_Temp.prototype.initialize;
Game_Temp.prototype.initialize = function(){
	CP.MagicWeaponLevelUpInfo.TEMP_INIT.call(this);
	this.clearMagicWeaponLevelUpInfo();
};

Game_Temp.prototype.clearMagicWeaponLevelUpInfo = function(){
	this._magicWeaponLevelUpInfo = [];
};

//信息数量
Game_Temp.prototype.magicWeaponLevelUpInfoSize = function(){
	return this._magicWeaponLevelUpInfo.length;
};

//存在性
Game_Temp.prototype.hasMagicWeaponLevelUpInfo = function(){
	return this.magicWeaponLevelUpInfoSize() > 0;
};

//添加
Game_Temp.prototype.addMagicWeaponLevelUpInfo = function(item){
	if(!!item)
		this._magicWeaponLevelUpInfo.push(item);
};

//读取当前，并移除他
Game_Temp.prototype.currentMagicWeaponLevelUpInfo = function(){
	if(this.hasMagicWeaponLevelUpInfo())
		return this._magicWeaponLevelUpInfo.pop();
	else
		return null;
};

//------------------------------
// Game_Item
//------------------------------
CP.MagicWeaponLevelUpInfo.ITEM_PROCESS_LEVEL_UP_EFFECT = Game_Item.prototype.processLevelUpEffect;
Game_Item.prototype.processLevelUpEffect = function(){
	$gameTemp.addMagicWeaponLevelUpInfo(this.object());
	CP.MagicWeaponLevelUpInfo.ITEM_PROCESS_LEVEL_UP_EFFECT.call(this);
};

//------------------------------
// Window_MagicWeaponLevelUpInfo
//------------------------------
function Window_MagicWeaponLevelUpInfo(){
	this.initialize.apply(this, arguments);
};

Window_MagicWeaponLevelUpInfo.prototype = Object.create(Window_Selectable.prototype);
Window_MagicWeaponLevelUpInfo.prototype.constructor = Window_MagicWeaponLevelUpInfo;

Window_MagicWeaponLevelUpInfo.prototype.initialize = function(x, y, width, maxLines){
	this._data = [];
	Window_Selectable.prototype.initialize.call(this, x, y, width, this.fittingHeight(maxLines));
	this.opacity = 0;
};

//项目数量
Window_MagicWeaponLevelUpInfo.prototype.maxItems = function() {
    return this._data.length;
};

//是否有数据记录
Window_MagicWeaponLevelUpInfo.prototype.hasData = function(){
	return !!this._data && this._data.length > 0;
};

//这个道具是否已经记录
Window_MagicWeaponLevelUpInfo.prototype.hasExisted = function(item){
	for(var i = 0; i < this._data.length; i++){
		if(this._data[i].item === item)
			return true;
	}
	return false;
};

//添加数据记录，显示时间count帧
Window_MagicWeaponLevelUpInfo.prototype.addData = function(item, count){
	if(!this.hasExisted(item))
		this._data.unshift({item: item, count: count});
};

Window_MagicWeaponLevelUpInfo.prototype.drawItem = function(index){
	var item = this._data[index].item;
	var rect = this.itemRect(index);
	this.drawItemBackground(rect);
	this.drawItemData(item, rect);
};

Window_MagicWeaponLevelUpInfo.prototype.drawItemBackground = function(rect){
	/*
	if(this.canDrawBackground()){
		var color1 = this.backgroundColor1();
		var color2 = this.backgroundColor2();
		this.contents.gradientFillRect(rect.x, rect.y, rect.width / 2, rect.height, color2, color1);
	    this.contents.gradientFillRect(rect.x + rect.width / 2, rect.y, rect.width / 2, rect.height
	    	, color1, color2);
	}*/
	if(this.canDrawBackground()){
		const color = 'rgba(0, 0, 0, 0.3)';
		const padding = 4;
		this.contents.fillRect(rect.x, rect.y, rect.width, rect.height, color);
		this.contents.fillRect(rect.x + padding, rect.y + padding, rect.width - padding * 2, rect.height - padding * 2, color);
	}
};

Window_MagicWeaponLevelUpInfo.prototype.canDrawBackground = function(){
	return CP.MagicWeaponLevelUpInfo.BACKGROUND_COLOR1 >= 0
		&& CP.MagicWeaponLevelUpInfo.BACKGROUND_COLOR2 >= 0;
};

Window_MagicWeaponLevelUpInfo.prototype.backgroundColor1 = function(){
	return this.textColor(CP.MagicWeaponLevelUpInfo.BACKGROUND_COLOR1);
};

Window_MagicWeaponLevelUpInfo.prototype.backgroundColor2 = function(){
	return this.textColor(CP.MagicWeaponLevelUpInfo.BACKGROUND_COLOR2);
};

Window_MagicWeaponLevelUpInfo.prototype.drawItemData = function(item, rect){
	var gameItem = new Game_Item(item);
	var text = this.magicWeaponLevelUpInfo(item).format(item.name, gameItem.level());
	text = '\\C[0]' + text;	//不懂为啥旧存档显示的颜色有问题，强行补个颜色符。
	var tw = this.textWidthEx(text);
	this.resetFontSettings();
	this.changeTextColor(this.dataTextColor());
	var dx = rect.x + (rect.width-tw)/2;
	this.drawTextEx(text, dx, rect.y);
};

Window_MagicWeaponLevelUpInfo.prototype.magicWeaponLevelUpInfo = function(item){
	var baseItem = DataManager.getBaseItem(item);
	if(!!baseItem)
		return baseItem.magicWeaponLevelUpInfo;
	else
		return "";
};

Window_MagicWeaponLevelUpInfo.prototype.dataTextColor = function(){
	return this.textColor(CP.MagicWeaponLevelUpInfo.TEXT_COLOR);
};

Window_MagicWeaponLevelUpInfo.prototype.update = function(){
	this.updateTempData();
	this.updateDataCount();
	this.updateVisibleState();
	Window_Selectable.prototype.update.call(this);
};

//更新临时对象数据 转移到窗口
Window_MagicWeaponLevelUpInfo.prototype.updateTempData = function(){
	var item = $gameTemp.currentMagicWeaponLevelUpInfo();
	if(!!item){
		this.addData(item, CP.MagicWeaponLevelUpInfo.INFO_TIME);
		this.refresh();
	}
};

//更新数据计时
Window_MagicWeaponLevelUpInfo.prototype.updateDataCount = function(){
	var length = this._data.length;
	for(var i = 0; i < length; i++){
		if(!!this._data[i] && this._data[i].count > 0)
			this._data[i].count--;
	}
	this._data = this._data.filter(function(data){
		return !!data && data.count > 0;
	});
	if(this._data.length !== length)
		this.refresh();
};

//刷新显示状态
Window_MagicWeaponLevelUpInfo.prototype.updateVisibleState = function(){
	if(this.hasData())
		this.show();
	else
		this.hide();
};

//------------------------------
// Scene_Base
//------------------------------
Scene_Base.prototype.createMagicWeaponLevelUpInfoWindow = function(){
	var x = Math.floor(CP.MagicWeaponLevelUpInfo.WINDOW_RECT.x.call(this));
	var y = Math.floor(CP.MagicWeaponLevelUpInfo.WINDOW_RECT.y.call(this));
	var width = Math.floor(CP.MagicWeaponLevelUpInfo.WINDOW_RECT.width.call(this));
	var maxLines = CP.MagicWeaponLevelUpInfo.WINDOW_RECT.maxLines;

	this._magicWeaponLevelUpInfoWindow = new Window_MagicWeaponLevelUpInfo(x, y, width, maxLines);
	this.addChild(this._magicWeaponLevelUpInfoWindow);
};

//------------------------------
// Scene_Map
//------------------------------
CP.MagicWeaponLevelUpInfo.SCENE_MAP_CREATE_ALL_WINDOWS = Scene_Map.prototype.createAllWindows;
Scene_Map.prototype.createAllWindows = function(){
	CP.MagicWeaponLevelUpInfo.SCENE_MAP_CREATE_ALL_WINDOWS.call(this);
	this.createMagicWeaponLevelUpInfoWindow();
};

//------------------------------
// Scene_Battle
//------------------------------
CP.MagicWeaponLevelUpInfo.SCENE_BATTLE_CREATE_ALL_WINDOWS = Scene_Battle.prototype.createAllWindows;
Scene_Battle.prototype.createAllWindows = function(){
	CP.MagicWeaponLevelUpInfo.SCENE_BATTLE_CREATE_ALL_WINDOWS.call(this);
	this.createMagicWeaponLevelUpInfoWindow();
};